<!--This jsp file is activated when a user presses the add_course button from 
	courses.jsp -->
<%@page import="sql.DBManagment"%>
<%@page import="sql.record.*"%>
<%@page import="java.util.*"%>
<%	
	User loginUser = new User();
	loginUser.userName = (String) session.getAttribute("loggedUser");
	User storedUser = new User();
	try  {
		List<User> userList = DBManagment.getRecord(loginUser);
		storedUser = userList.get(0);
	}catch(Exception e){
		%>
		<script type = "text/javascript" src = "script/end_session.js"></script>		
		<%
	}
 %>
<%
User currentUser = new User();
currentUser.userName = (String) session.getAttribute("loggedUser");
List<TableRecord> courseSchedList = DBManagment.getAvailableCourses(currentUser);

//will get all courses
List<Course> courses = new LinkedList<Course>();
for(TableRecord t: courseSchedList){
	if(t instanceof Course){
		int lastIndex = courses.size() - 1;
		Course currentCourse = (Course)t;
		if((lastIndex == -1) || (!courses.get(lastIndex).courseName.equals(currentCourse.courseName))){
			courses.add((Course)t);
		}
	}
}
%>
<script type="text/javascript" src = "script/add_courses.js"></script>
<script type="text/javascript" src = "script/forms.js"></script>
<div class="bodytext" style="padding:12px;" align="justify">

	<form name="mngForm" id="mngForm" class="tableForm">
		<fieldset>
			<legend>Control Box</legend>
			<input type="checkbox" name="srchDB" id="srchDB" />
			<span class="checkBoxOption"><b>Search DB</b></span>
			<br/>
			<input type="text" name="AJAXsearchBox" id="AJAXsearchBox" />
			<input type="text" name="searchBox" id="searchBox" /><br/>
			<label for="srchOptions">Also Search By </label>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Credit Points</span>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Capacity</span>
			<input type="checkbox" name="srchOptions" id="srchOptions" />
			<span class="checkBoxOption">Description</span><br/>
			<br/>
			<input type="button" id="markAll" value="Mark All" />
			<input type="button" id="UnMarkAll" value="Unmark All" />
			<input type="button" id="registerMarked" value="Register" class = "handleMarked"/>
		</fieldset>
		<div id = "CoursesDiv">
		<%
			for(Course c: courses){
		%>		
			<div class="panel" align="justify">
			<span class="orangetitle">
				<input type="checkbox" name="mark" id="mark" value=<%="\"" + c.courseName + "\""%> />
		      	<%=c.courseName%>
		      	<input type="hidden" value=<%="\"" + c.courseName + "\""%> class="hiddenData" />
		    </span>
			<div class="bodytext"><br />
				<ul>
					<li><b>Course Name: </b><%=c.courseName%></li>
					<li>
						<b>Credit Points: </b><%=c.creditPoints%>
						<input type="hidden" value=<%="\"" + c.creditPoints + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Capacity: </b><%=c.capacity%>
						<input type="hidden" value=<%="\"" + c.capacity + "\""%> class="hiddenData" />
					</li>
					<li>
						<b>Description: </b><br/><%=c.description%>
						<input type="hidden" value=<%="\"" + c.description + "\""%> class="hiddenData" />
					</li>
					<li>
						<strong>Schedule</strong>
						<ul>
						<%
							String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday"};
							for(TableRecord t : courseSchedList){
								if((t instanceof CourseSchedule) && (((CourseSchedule)t).courseName.equals(c.courseName))){
								CourseSchedule currentSched = ((CourseSchedule)t);
							%>
								<%
									String startTime = currentSched.startHour.toString();
									String endTime = currentSched.endHour.toString();
									if(currentSched.startHour.intValue() < 1000){
										startTime = "0" + startTime;
									}
									if(currentSched.endHour.intValue() < 1000){
										endTime = "0" + endTime;
									}							
								%>
								<li><%=days[currentSched.day]%>, <%=startTime%> to <%=endTime%> </li>
							<%		
								}
							}
						%>
						</ul>
					</li>
				</ul>
		    </div>
		</div>
		<%
			}
		%>
		</div>
	</form>
</div>

